package com.cloud.pole.shop.biz.order;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cloud.pole.common.biz.BaseBiz;
import com.cloud.pole.mapper.order.ShopOrderRefundMapper;
import com.cloud.pole.shop.model.entity.order.ShopOrderRefund;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;
/**
 * ${DESCRIPTION}
 *
 * @author wanghaobin
 * @create 2017-06-08 16:23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ShopOrderRefundBiz extends BaseBiz<ShopOrderRefundMapper, ShopOrderRefund> {
	
	@Autowired
	ShopOrderRefundMapper shopOrderRefundMapper;
	
	public void insertUseGeneratedKeys(ShopOrderRefund entity) {
		shopOrderRefundMapper.insertUseGeneratedKeys(entity);
	}

	 
	@Override
    public void insertSelective(ShopOrderRefund entity) {
        super.insertSelective(entity);
    }
	
	public ShopOrderRefund selectByPrimaryKey(Long id) {
		return super.selectById(id);
	}
	
	public void updateSelectiveById(ShopOrderRefund entity) {
	       super.updateSelectiveById(entity);
	}
	
    public List<ShopOrderRefund> selectOrderRefundListByUserId(Long userId) {
		 Example example = new Example(ShopOrderRefund.class);
		 Criteria criteria = example.createCriteria();
		 criteria.andEqualTo("userId", userId);
		 example.setOrderByClause("refund_id desc");
		 return selectByExample(example);
    }
    
    public int selectOrderRefundCountByUserId(Long userId) {
		 Example example = new Example(ShopOrderRefund.class);
		 Criteria criteria = example.createCriteria();
		 criteria.andEqualTo("userId", userId);
		 return selectCountByExample(example);
   }


	public List<ShopOrderRefund> selectPageShopOrderRefundAdminList(Map<String, String> map) {
		 String searchType = map.get("searchType");
		 String orderSn = map.get("orderSn");
		 String refundId = map.get("refundId");
		 String refundWay = map.get("refundWay");
		 String refundStatus = map.get("refundStatus");
		 String startTime = map.get("startTime");
		 String endTime = map.get("refundStatus");
		 
		Example example = new Example(ShopOrderRefund.class);
		 Criteria criteria = example.createCriteria();
		 if("1".equals(searchType)){
			 criteria.andCondition(" refund_status=20");
		 }else if("2".equals(searchType)){
			 criteria.andCondition(" refund_status=1 or refund_status=30 ");
		 }
		 if(!StringUtils.isBlank(orderSn.trim())){
			 criteria.andEqualTo("orderSn", orderSn);
		 }
		 if(!StringUtils.isBlank(refundId.trim())){
			 criteria.andEqualTo("refundId", refundId);
		 }
		 if(!StringUtils.isBlank(refundWay.trim())){
			 criteria.andEqualTo("refundWay", refundWay);
		 }
		 if(!StringUtils.isBlank(refundStatus.trim())){
			 criteria.andEqualTo("refundStatus", refundStatus);
		 }
		 if(!StringUtils.isBlank(startTime.trim()) && !StringUtils.isBlank(endTime.trim())){
			 criteria.andBetween("createTime", startTime, endTime);
		 }
		 example.setOrderByClause("refund_id desc");
		 return selectByExample(example);
	}
    
}
